#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/8/15 18:29
# @Author  : youfeng
import os
import signal
import time

from ext.update_service import UpdateService
from logger import AppLogger

log = AppLogger('gateway_proxy_uppdate.log').get_logger()

# 当前进程运行状态
is_running = True


def process_quit(signo, frame):
    global is_running
    is_running = False
    log.info("收到退出进程信号...")


def main():
    # 注册优雅退出
    signal.signal(signal.SIGINT, process_quit)
    signal.signal(signal.SIGTERM, process_quit)
    signal.signal(signal.SIGQUIT, process_quit)
    signal.signal(signal.SIGUSR1, process_quit)

    base_dir = os.path.abspath(os.path.dirname(__file__))
    scheduler = UpdateService(base_path=base_dir, task_prefix="proxys", log=log)
    while is_running:
        start_time = time.time()
        scheduler.start()
        time.sleep(10)
        log.info("更新周期耗时: {} s".format(round(time.time() - start_time, 2)))

    log.info("退出代理更新进程完成...")


if __name__ == '__main__':
    main()
